package main

import (
	"os"
	"os/signal"
	"log"
	"github.com/valyala/fasthttp"
	"gopkg.in/alecthomas/kingpin.v2"
)

var (
	basepath = kingpin.Flag("path", "base path").Default(".").String()
	port     = kingpin.Flag("port", "port to listen").Default("8000").Short('p').String()
	logfile  = kingpin.Flag("log", "log file").Default("crm-query.log").Short('l').String()
)

func main() {
	kingpin.Parse()
	srv := Server{logicLayer: Logic{}}
	srv.logicLayer.Initial()
	srv.logicLayer.CloseDBEveryDay()
	// catch system call and excute Finalize()
    signalChan := make(chan os.Signal, 1)
	signal.Notify(signalChan)
    go func() {
        a := <-signalChan
		log.Printf("[Info]: Receive signal %v", a)
        srv.logicLayer.Finalize()
        os.Exit(0)
    }()
    // signal.Notify(signalChan)
	fasthttp.ListenAndServe(":"+*port, srv.Handle)
}
